/*
 *  CSE 40166, Computer Graphics, Fall 2013
 *
 *  PORT
 *  File: ship.cpp
 *
 *  Kevin Sonnen
 *
 *  Description: Class representing the player's ship in the game. Derived from vehicle class
 *
 */
 
#include "ship.h"

//
// ship()
//
// Constructor for the ship class, initializes position and some other variables
//
ship::ship() {
	// Initialize position to (0, 0)
	position.push_back(0);
	position.push_back(0);
	velocity.push_back(0);
	velocity.push_back(0);
	vehicleSpeed = 18;
	rotationAngle = 0.0f;
	//rotationSpeed = 10.0f;
	maxTeleportationDistance = 300;
	radius = 40;
	gameWidth = 512;
	gameHeight = 512;
	alive = true;
}

ship::~ship() {

}

//
// draw()
//
// Draw the ship. It ends up looking similar to a shark tooth.
//
void ship::draw() {
	const bool moveCamera = false;
	if(!moveCamera)
		glPushMatrix();
	
	//glColor3f( 0.0f, 1.0f, 0.0f );
	const int shipWidth = 90;
	const int shipHeight = 100;
	
	const GLfloat shipPoints[14][2] = {
		{ 0.0, shipHeight/2 },			// Pt 0
		{ -shipWidth/10, shipHeight/4 },	// Pt 1
		{ shipWidth/10, shipHeight/4 },		// Pt 2
		{ -shipWidth/5, 0.0 },			// Pt 3
		{ shipWidth/5, 0.0 },			// Pt 4
		{ -shipWidth/3, -shipHeight/4 },	// Pt 5
		{ 0.0, -shipHeight/4 },			// Pt 6
		{ shipWidth/3, -shipHeight/4 },		// Pt 7
		{ -shipWidth/2, -shipHeight/2 },	// Pt 8
		{ -shipWidth/4, -shipHeight/3 },	// Pt 9
		{ shipWidth/4, -shipHeight/3 },		// Pt 10
		{ shipWidth/2, -shipHeight/2 },		// Pt 11
		{ 0.0, 0.0 },				// Pt 12
		{ 0.0, shipHeight/4 }};			// Pt 12
	if(!moveCamera) {
		glTranslatef( position[0], position[1], 0 );
		glRotatef( (180.0*rotationAngle/M_PI)-90, 0, 0, 1);
	}
	
	glBegin( GL_TRIANGLES ); {
		// Draw Top Left Triangle
		glColor3f( 0.0f, 1.0f, 0.0f );
		glVertex2fv(shipPoints[0]);
		glVertex2fv(shipPoints[13]);
		glColor3f( 0.0f, 0.6f, 0.0f );
		glVertex2fv(shipPoints[1]);
		
		// Draw the top Right Triangle
		glColor3f( 0.0f, 1.0f, 0.0f );
		glVertex2fv(shipPoints[0]);
		glVertex2fv(shipPoints[13]);
		glColor3f( 0.0f, 0.6f, 0.0f );
		glVertex2fv(shipPoints[2]);
		
		// Draw bottom left triangle
		glColor3f( 0.0f, 0.1f, 0.0f );
		glVertex2fv(shipPoints[8]);
		glColor3f( 0.0f, 0.3f, 0.0f );
		glVertex2fv(shipPoints[5]);
		glVertex2fv(shipPoints[9]);
		
		// Draw bottom right triangle
		glColor3f( 0.0f, 0.1f, 0.0f );
		glVertex2fv(shipPoints[11]);
		glColor3f( 0.0f, 0.3f, 0.0f );
		glVertex2fv(shipPoints[7]);
		glVertex2fv(shipPoints[10]);
		
		// Draw Center-Left Triangle on the bottom
		glColor3f( 0.0f, 0.3f, 0.0f );
		glVertex2fv(shipPoints[5]);
		glVertex2fv(shipPoints[9]);
		glColor3f( 0.0f, 1.0f, 0.0f );		
		glVertex2fv(shipPoints[6]);
		
		// Draw Center-Right Triangle on the bottom
		glColor3f( 0.0f, 0.3f, 0.0f );
		glVertex2fv(shipPoints[7]);
		glVertex2fv(shipPoints[10]);
		glColor3f( 0.0f, 1.0f, 0.0f );
		glVertex2fv(shipPoints[6]);
	} glEnd();
	
	glBegin( GL_QUADS ); {
		// Draw upper left quad
		glColor3f( 0.0f, 0.6f, 0.0f );
		glVertex2fv(shipPoints[1]);
		glColor3f( 0.0f, 0.4f, 0.0f );		
		glVertex2fv(shipPoints[3]);
		glColor3f( 0.0f, 1.0f, 0.0f );
		glVertex2fv(shipPoints[12]);
		glVertex2fv(shipPoints[13]);
		
		// Draw upper right quad
		glColor3f( 0.0f, 0.6f, 0.0f );
		glVertex2fv(shipPoints[2]);
		glColor3f( 0.0f, 0.4f, 0.0f );
		glVertex2fv(shipPoints[4]);
		glColor3f( 0.0f, 1.0f, 0.0f );
		glVertex2fv(shipPoints[12]);
		glVertex2fv(shipPoints[13]);
		
		// Draw lower left quad
		glColor3f( 0.0f, 0.3f, 0.0f );
		glVertex2fv(shipPoints[5]);
		glColor3f( 0.0f, 0.4f, 0.0f );
		glVertex2fv(shipPoints[3]);
		glColor3f( 0.0f, 1.0f, 0.0f );
		glVertex2fv(shipPoints[12]);
		glVertex2fv(shipPoints[6]);
		
		// Draw the lower right quad
		glColor3f( 0.0f, 0.3f, 0.0f );
		glVertex2fv(shipPoints[7]);
		glColor3f( 0.0f, 0.4f, 0.0f );
		glVertex2fv(shipPoints[4]);
		glColor3f( 0.0f, 1.0f, 0.0f );
		glVertex2fv(shipPoints[12]);
		glVertex2fv(shipPoints[6]);
	} glEnd();
	
	if(moveCamera) {
		// Put this after the drawing and reverse the position so that the camera stays centered on the player's ship
		glTranslatef( -position[0], -position[1], 0 );
		//glRotatef( rotationAngle, 0, 0, 1);
	}
	if(!moveCamera)
		glPopMatrix();
}

//
// void upDatePosition()
//
// Update the x and y positions based on the velocity, keep the ship in the game screen
//
void ship::updatePosition() {
	if (position[0] + velocity[0] - radius > -gameWidth && position[0] + velocity[0]  + radius < gameWidth)
		position[0] += velocity[0];
	if (position[1] + velocity[1] - radius > -gameHeight && position[1] + velocity[1] + radius < gameHeight)
		position[1] += velocity[1];
	//rotationAngle += rotationVelocity;
}

//
// void upDatePosition()
//
// Update the x and y positions, keep the ship in the game screen
//
void ship::teleportToPoint(const int &x, const int &y) {
	if (position[0] + x - radius > -gameWidth && position[0] + x + radius < gameWidth)
		position[0] = position[0]+x;
	if (position[1] + y - radius > -gameHeight && position[1] + y + radius < gameHeight)
		position[1] = position[1]+y;
}

/*void ship::teleportForward() {
	position[1] += maxTeleportationDistance;
}

void ship::teleportBackward() {
	position[1] -= maxTeleportationDistance;
}

void ship::teleportLeft() {
	position[0] -= maxTeleportationDistance;
}

void ship::teleportRight() {
	position[0] += maxTeleportationDistance;
}*/
